{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Visualization: Trading Session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import altair as alt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Define parameters and Load model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from trading_bot.agent import Agent\n",
    "\n",
    "model_name = 'model_GOOG_50'\n",
    "test_stock = 'data/GOOG_2019.csv'\n",
    "window_size = 10\n",
    "debug = True\n",
    "\n",
    "agent = Agent(window_size, pretrained=True, model_name=model_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Load test data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>actual</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-02</td>\n",
       "      <td>1045.849976</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-03</td>\n",
       "      <td>1016.059998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019-01-04</td>\n",
       "      <td>1070.709961</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019-01-07</td>\n",
       "      <td>1068.390015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019-01-08</td>\n",
       "      <td>1076.280029</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        date       actual\n",
       "0 2019-01-02  1045.849976\n",
       "1 2019-01-03  1016.059998\n",
       "2 2019-01-04  1070.709961\n",
       "3 2019-01-07  1068.390015\n",
       "4 2019-01-08  1076.280029"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# read csv into dataframe\n",
    "df = pd.read_csv(test_stock)\n",
    "# filter out the desired features\n",
    "df = df[['Date', 'Adj Close']]\n",
    "# rename feature column names\n",
    "df = df.rename(columns={'Adj Close': 'actual', 'Date': 'date'})\n",
    "# convert dates from object to DateTime type\n",
    "dates = df['date']\n",
    "dates = pd.to_datetime(dates, infer_datetime_format=True)\n",
    "df['date'] = dates\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Running Eval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG switching to TensorFlow for CPU\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1057.19\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1080.97\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1089.90 | Position: +$32.71\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1098.26 | Position: +$17.29\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1073.90\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1090.99 | Position: +$17.09\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1098.71\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1095.06\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1095.01\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1121.37 | Position: +$22.66\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1120.16\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1113.65\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1118.56\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1113.80\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1096.97\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1115.13 | Position: +$20.07\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1116.05 | Position: +$21.04\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1119.92 | Position: -$0.24\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1140.99 | Position: +$27.34\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1147.80 | Position: +$29.24\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1162.03 | Position: +$48.23\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1157.86 | Position: +$60.89\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1185.55\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1184.46\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1184.26 | Position: -$1.29\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1198.85\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1223.97 | Position: +$39.51\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1231.54 | Position: +$32.69\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1193.00\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1184.62 | Position: -$8.38\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1173.02\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1168.49 | Position: -$4.53\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1207.15\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1203.84\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1197.25\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1202.16 | Position: -$4.99\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1204.62 | Position: +$0.78\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1217.87 | Position: +$20.62\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1162.61\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1185.40 | Position: +$22.79\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1174.10\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1166.27\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1164.27 | Position: -$9.83\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1120.44\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1164.21\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1178.98\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1149.63\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1151.42 | Position: -$14.85\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1140.77 | Position: +$20.33\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1133.47\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1134.15\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1116.46\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1117.95\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1103.63\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1036.23\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1053.05 | Position: -$111.16\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1042.22\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1044.34\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1080.38 | Position: -$98.60\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1078.72\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1077.03 | Position: -$72.60\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1088.77\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1085.35 | Position: -$48.12\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1092.50 | Position: -$41.65\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1103.60\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1102.33 | Position: -$14.13\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1111.42 | Position: -$6.53\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1121.88\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1115.52\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1086.35\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1079.80\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1076.01\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1080.91\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1097.95 | Position: -$5.68\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1111.25\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1121.58 | Position: +$85.35\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1131.59 | Position: +$89.37\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1116.35\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1124.83 | Position: +$80.49\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1140.48 | Position: +$61.76\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1144.21 | Position: +$55.44\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1144.90 | Position: +$41.30\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1150.34 | Position: +$28.46\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1153.58 | Position: +$38.06\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1146.35 | Position: +$60.00\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1146.33 | Position: +$66.53\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1130.10 | Position: +$54.09\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1138.07 | Position: +$57.16\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1146.21 | Position: +$34.96\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1137.81\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1132.12 | Position: +$15.77\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1250.41 | Position: +$112.60\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1225.14\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1216.68\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1209.01\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1193.99\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1173.99\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1204.80 | Position: -$20.34\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1188.01\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1197.27\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1164.29\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1167.26\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1177.60\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1198.45 | Position: -$18.23\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1182.69 | Position: -$26.32\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1189.53\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1151.29 | Position: -$42.70\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1168.89\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1167.84 | Position: -$6.15\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1171.02 | Position: -$16.99\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1192.85 | Position: -$4.42\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1188.10 | Position: +$23.81\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1168.39 | Position: +$1.13\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1181.41 | Position: +$3.81\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1211.38 | Position: +$21.85\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1204.93 | Position: +$36.04\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1204.41\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1206.00 | Position: +$1.59\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1229.15\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1232.41 | Position: +$3.26\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1225.09\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1219.00 | Position: -$6.09\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1209.00\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1207.68 | Position: -$1.32\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1189.13\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1202.31\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1208.67 | Position: +$19.54\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1215.45\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1243.01 | Position: +$40.70\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1243.64 | Position: +$28.19\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1245.49\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Buy at: $1246.15\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1242.80 | Position: -$2.69\n",
      "2020-01-11 01:09:29 192.168.1.14 root[4294] DEBUG Sell at: $1259.13 | Position: +$12.98\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1291.37\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1292.03 | Position: +$0.66\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1303.05\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1301.35\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1295.34\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1313.55\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1312.99\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1304.96\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1289.92\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1295.28 | Position: -$7.77\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1328.13 | Position: +$26.78\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1340.62 | Position: +$45.28\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1343.56 | Position: +$30.01\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1344.66 | Position: +$31.67\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1345.02 | Position: +$40.06\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1350.27 | Position: +$60.35\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Buy at: $1348.84\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] DEBUG Sell at: $1343.56 | Position: -$5.28\n",
      "2020-01-11 01:09:30 192.168.1.14 root[4294] INFO model_GOOG_50: +$1141.45\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import logging\n",
    "import coloredlogs\n",
    "\n",
    "from trading_bot.utils import show_eval_result, switch_k_backend_device, get_stock_data\n",
    "from trading_bot.methods import evaluate_model\n",
    "\n",
    "coloredlogs.install(level='DEBUG')\n",
    "switch_k_backend_device()\n",
    "\n",
    "test_data = get_stock_data(test_stock)\n",
    "initial_offset = test_data[1] - test_data[0]\n",
    "\n",
    "test_result, history = evaluate_model(agent, test_data, window_size, debug)\n",
    "show_eval_result(model_name, test_result, initial_offset)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4. Visualize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def visualize(df, history, title=\"trading session\"):\n",
    "    # add history to dataframe\n",
    "    position = [history[0][0]] + [x[0] for x in history]\n",
    "    actions = ['HOLD'] + [x[1] for x in history]\n",
    "    df['position'] = position\n",
    "    df['action'] = actions\n",
    "    \n",
    "    # specify y-axis scale for stock prices\n",
    "    scale = alt.Scale(domain=(min(min(df['actual']), min(df['position'])) - 50, max(max(df['actual']), max(df['position'])) + 50), clamp=True)\n",
    "    \n",
    "    # plot a line chart for stock positions\n",
    "    actual = alt.Chart(df).mark_line(\n",
    "        color='green',\n",
    "        opacity=0.5\n",
    "    ).encode(\n",
    "        x='date:T',\n",
    "        y=alt.Y('position', axis=alt.Axis(format='$.2f', title='Price'), scale=scale)\n",
    "    ).interactive(\n",
    "        bind_y=False\n",
    "    )\n",
    "    \n",
    "    # plot the BUY and SELL actions as points\n",
    "    points = alt.Chart(df).transform_filter(\n",
    "        alt.datum.action != 'HOLD'\n",
    "    ).mark_point(\n",
    "        filled=True\n",
    "    ).encode(\n",
    "        x=alt.X('date:T', axis=alt.Axis(title='Date')),\n",
    "        y=alt.Y('position', axis=alt.Axis(format='$.2f', title='Price'), scale=scale),\n",
    "        color='action'\n",
    "    ).interactive(bind_y=False)\n",
    "\n",
    "    # merge the two charts\n",
    "    chart = alt.layer(actual, points, title=title).properties(height=300, width=1000)\n",
    "    \n",
    "    return chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<div id=\"altair-viz-2\"></div>\n",
       "<script type=\"text/javascript\">\n",
       "  (function(spec, embedOpt){\n",
       "    const outputDiv = document.getElementById(\"altair-viz-2\");\n",
       "    const paths = {\n",
       "      \"vega\": \"https://cdn.jsdelivr.net/npm//vega@5?noext\",\n",
       "      \"vega-lib\": \"https://cdn.jsdelivr.net/npm//vega-lib?noext\",\n",
       "      \"vega-lite\": \"https://cdn.jsdelivr.net/npm//vega-lite@4.0.0?noext\",\n",
       "      \"vega-embed\": \"https://cdn.jsdelivr.net/npm//vega-embed@6?noext\",\n",
       "    };\n",
       "\n",
       "    function loadScript(lib) {\n",
       "      return new Promise(function(resolve, reject) {\n",
       "        var s = document.createElement('script');\n",
       "        s.src = paths[lib];\n",
       "        s.async = true;\n",
       "        s.onload = () => resolve(paths[lib]);\n",
       "        s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n",
       "        document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "      });\n",
       "    }\n",
       "\n",
       "    function showError(err) {\n",
       "      outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n",
       "      throw err;\n",
       "    }\n",
       "\n",
       "    function displayChart(vegaEmbed) {\n",
       "      vegaEmbed(outputDiv, spec, embedOpt)\n",
       "        .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n",
       "    }\n",
       "\n",
       "    if(typeof define === \"function\" && define.amd) {\n",
       "      requirejs.config({paths});\n",
       "      require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n",
       "    } else if (typeof vegaEmbed === \"function\") {\n",
       "      displayChart(vegaEmbed);\n",
       "    } else {\n",
       "      loadScript(\"vega\")\n",
       "        .then(() => loadScript(\"vega-lite\"))\n",
       "        .then(() => loadScript(\"vega-embed\"))\n",
       "        .catch(showError)\n",
       "        .then(() => displayChart(vegaEmbed));\n",
       "    }\n",
       "  })({\"config\": {\"view\": {\"continuousWidth\": 400, \"continuousHeight\": 300}}, \"layer\": [{\"mark\": {\"type\": \"line\", \"color\": \"green\", \"opacity\": 0.5}, \"encoding\": {\"x\": {\"type\": \"temporal\", \"field\": \"date\"}, \"y\": {\"type\": \"quantitative\", \"axis\": {\"format\": \"$.2f\", \"title\": \"Price\"}, \"field\": \"position\", \"scale\": {\"clamp\": true, \"domain\": [966.0599980000001, 1411.170044]}}}, \"selection\": {\"selector003\": {\"type\": \"interval\", \"bind\": \"scales\", \"encodings\": [\"x\"]}}}, {\"mark\": {\"type\": \"point\", \"filled\": true}, \"encoding\": {\"color\": {\"type\": \"nominal\", \"field\": \"action\"}, \"x\": {\"type\": \"temporal\", \"axis\": {\"title\": \"Date\"}, \"field\": \"date\"}, \"y\": {\"type\": \"quantitative\", \"axis\": {\"format\": \"$.2f\", \"title\": \"Price\"}, \"field\": \"position\", \"scale\": {\"clamp\": true, \"domain\": [966.0599980000001, 1411.170044]}}}, \"selection\": {\"selector004\": {\"type\": \"interval\", \"bind\": \"scales\", \"encodings\": [\"x\"]}}, \"transform\": [{\"filter\": \"(datum.action !== 'HOLD')\"}]}], \"data\": {\"name\": \"data-d5689d94cf7723a01a17282c2d54976a\"}, \"height\": 300, \"title\": \"data/GOOG_2019.csv\", \"width\": 1000, \"$schema\": \"https://vega.github.io/schema/vega-lite/v4.0.0.json\", \"datasets\": {\"data-d5689d94cf7723a01a17282c2d54976a\": [{\"date\": \"2019-01-02T00:00:00\", \"actual\": 1045.849976, \"position\": 1045.849976, \"action\": \"HOLD\"}, {\"date\": \"2019-01-03T00:00:00\", \"actual\": 1016.0599980000001, \"position\": 1045.849976, \"action\": \"HOLD\"}, {\"date\": \"2019-01-04T00:00:00\", \"actual\": 1070.709961, \"position\": 1016.0599980000001, \"action\": \"HOLD\"}, {\"date\": \"2019-01-07T00:00:00\", \"actual\": 1068.390015, \"position\": 1070.709961, \"action\": \"HOLD\"}, {\"date\": \"2019-01-08T00:00:00\", \"actual\": 1076.280029, \"position\": 1068.390015, \"action\": \"HOLD\"}, {\"date\": \"2019-01-09T00:00:00\", \"actual\": 1074.660034, \"position\": 1076.280029, \"action\": \"HOLD\"}, {\"date\": \"2019-01-10T00:00:00\", \"actual\": 1070.329956, \"position\": 1074.660034, \"action\": \"HOLD\"}, {\"date\": \"2019-01-11T00:00:00\", \"actual\": 1057.189941, \"position\": 1070.329956, \"action\": \"HOLD\"}, {\"date\": \"2019-01-14T00:00:00\", \"actual\": 1044.689941, \"position\": 1057.189941, \"action\": \"BUY\"}, {\"date\": \"2019-01-15T00:00:00\", \"actual\": 1077.150024, \"position\": 1044.689941, \"action\": \"HOLD\"}, {\"date\": \"2019-01-16T00:00:00\", \"actual\": 1080.969971, \"position\": 1077.150024, \"action\": \"HOLD\"}, {\"date\": \"2019-01-17T00:00:00\", \"actual\": 1089.900024, \"position\": 1080.969971, \"action\": \"BUY\"}, {\"date\": \"2019-01-18T00:00:00\", \"actual\": 1098.26001, \"position\": 1089.900024, \"action\": \"SELL\"}, {\"date\": \"2019-01-22T00:00:00\", \"actual\": 1070.52002, \"position\": 1098.26001, \"action\": \"SELL\"}, {\"date\": \"2019-01-23T00:00:00\", \"actual\": 1075.569946, \"position\": 1070.52002, \"action\": \"HOLD\"}, {\"date\": \"2019-01-24T00:00:00\", \"actual\": 1073.900024, \"position\": 1075.569946, \"action\": \"HOLD\"}, {\"date\": \"2019-01-25T00:00:00\", \"actual\": 1090.98999, \"position\": 1073.900024, \"action\": \"BUY\"}, {\"date\": \"2019-01-28T00:00:00\", \"actual\": 1070.079956, \"position\": 1090.98999, \"action\": \"SELL\"}, {\"date\": \"2019-01-29T00:00:00\", \"actual\": 1060.619995, \"position\": 1070.079956, \"action\": \"HOLD\"}, {\"date\": \"2019-01-30T00:00:00\", \"actual\": 1089.060059, \"position\": 1060.619995, \"action\": \"HOLD\"}, {\"date\": \"2019-01-31T00:00:00\", \"actual\": 1116.369995, \"position\": 1089.060059, \"action\": \"HOLD\"}, {\"date\": \"2019-02-01T00:00:00\", \"actual\": 1110.75, \"position\": 1116.369995, \"action\": \"HOLD\"}, {\"date\": \"2019-02-04T00:00:00\", \"actual\": 1132.800049, \"position\": 1110.75, \"action\": \"HOLD\"}, {\"date\": \"2019-02-05T00:00:00\", \"actual\": 1145.98999, \"position\": 1132.800049, \"action\": \"HOLD\"}, {\"date\": \"2019-02-06T00:00:00\", \"actual\": 1115.22998, \"position\": 1145.98999, \"action\": \"HOLD\"}, {\"date\": \"2019-02-07T00:00:00\", \"actual\": 1098.709961, \"position\": 1115.22998, \"action\": \"HOLD\"}, {\"date\": \"2019-02-08T00:00:00\", \"actual\": 1095.060059, \"position\": 1098.709961, \"action\": \"BUY\"}, {\"date\": \"2019-02-11T00:00:00\", \"actual\": 1095.01001, \"position\": 1095.060059, \"action\": \"BUY\"}, {\"date\": \"2019-02-12T00:00:00\", \"actual\": 1121.369995, \"position\": 1095.01001, \"action\": \"BUY\"}, {\"date\": \"2019-02-13T00:00:00\", \"actual\": 1120.160034, \"position\": 1121.369995, \"action\": \"SELL\"}, {\"date\": \"2019-02-14T00:00:00\", \"actual\": 1121.670044, \"position\": 1120.160034, \"action\": \"BUY\"}, {\"date\": \"2019-02-15T00:00:00\", \"actual\": 1113.650024, \"position\": 1121.670044, \"action\": \"HOLD\"}, {\"date\": \"2019-02-19T00:00:00\", \"actual\": 1118.560059, \"position\": 1113.650024, \"action\": \"BUY\"}, {\"date\": \"2019-02-20T00:00:00\", \"actual\": 1113.800049, \"position\": 1118.560059, \"action\": \"BUY\"}, {\"date\": \"2019-02-21T00:00:00\", \"actual\": 1096.969971, \"position\": 1113.800049, \"action\": \"BUY\"}, {\"date\": \"2019-02-22T00:00:00\", \"actual\": 1110.369995, \"position\": 1096.969971, \"action\": \"BUY\"}, {\"date\": \"2019-02-25T00:00:00\", \"actual\": 1109.400024, \"position\": 1110.369995, \"action\": \"HOLD\"}, {\"date\": \"2019-02-26T00:00:00\", \"actual\": 1115.130005, \"position\": 1109.400024, \"action\": \"HOLD\"}, {\"date\": \"2019-02-27T00:00:00\", \"actual\": 1116.050049, \"position\": 1115.130005, \"action\": \"SELL\"}, {\"date\": \"2019-02-28T00:00:00\", \"actual\": 1119.920044, \"position\": 1116.050049, \"action\": \"SELL\"}, {\"date\": \"2019-03-01T00:00:00\", \"actual\": 1140.98999, \"position\": 1119.920044, \"action\": \"SELL\"}, {\"date\": \"2019-03-04T00:00:00\", \"actual\": 1147.800049, \"position\": 1140.98999, \"action\": \"SELL\"}, {\"date\": \"2019-03-05T00:00:00\", \"actual\": 1162.030029, \"position\": 1147.800049, \"action\": \"SELL\"}, {\"date\": \"2019-03-06T00:00:00\", \"actual\": 1157.859985, \"position\": 1162.030029, \"action\": \"SELL\"}, {\"date\": \"2019-03-07T00:00:00\", \"actual\": 1143.300049, \"position\": 1157.859985, \"action\": \"SELL\"}, {\"date\": \"2019-03-08T00:00:00\", \"actual\": 1142.319946, \"position\": 1143.300049, \"action\": \"HOLD\"}, {\"date\": \"2019-03-11T00:00:00\", \"actual\": 1175.76001, \"position\": 1142.319946, \"action\": \"HOLD\"}, {\"date\": \"2019-03-12T00:00:00\", \"actual\": 1193.199951, \"position\": 1175.76001, \"action\": \"HOLD\"}, {\"date\": \"2019-03-13T00:00:00\", \"actual\": 1193.319946, \"position\": 1193.199951, \"action\": \"HOLD\"}, {\"date\": \"2019-03-14T00:00:00\", \"actual\": 1185.550049, \"position\": 1193.319946, \"action\": \"HOLD\"}, {\"date\": \"2019-03-15T00:00:00\", \"actual\": 1184.459961, \"position\": 1185.550049, \"action\": \"BUY\"}, {\"date\": \"2019-03-18T00:00:00\", \"actual\": 1184.26001, \"position\": 1184.459961, \"action\": \"BUY\"}, {\"date\": \"2019-03-19T00:00:00\", \"actual\": 1198.849976, \"position\": 1184.26001, \"action\": \"SELL\"}, {\"date\": \"2019-03-20T00:00:00\", \"actual\": 1223.969971, \"position\": 1198.849976, \"action\": \"BUY\"}, {\"date\": \"2019-03-21T00:00:00\", \"actual\": 1231.540039, \"position\": 1223.969971, \"action\": \"SELL\"}, {\"date\": \"2019-03-22T00:00:00\", \"actual\": 1205.5, \"position\": 1231.540039, \"action\": \"SELL\"}, {\"date\": \"2019-03-25T00:00:00\", \"actual\": 1193.0, \"position\": 1205.5, \"action\": \"HOLD\"}, {\"date\": \"2019-03-26T00:00:00\", \"actual\": 1184.619995, \"position\": 1193.0, \"action\": \"BUY\"}, {\"date\": \"2019-03-27T00:00:00\", \"actual\": 1173.02002, \"position\": 1184.619995, \"action\": \"SELL\"}, {\"date\": \"2019-03-28T00:00:00\", \"actual\": 1168.48999, \"position\": 1173.02002, \"action\": \"BUY\"}, {\"date\": \"2019-03-29T00:00:00\", \"actual\": 1173.310059, \"position\": 1168.48999, \"action\": \"SELL\"}, {\"date\": \"2019-04-01T00:00:00\", \"actual\": 1194.430054, \"position\": 1173.310059, \"action\": \"HOLD\"}, {\"date\": \"2019-04-02T00:00:00\", \"actual\": 1200.48999, \"position\": 1194.430054, \"action\": \"HOLD\"}, {\"date\": \"2019-04-03T00:00:00\", \"actual\": 1205.920044, \"position\": 1200.48999, \"action\": \"HOLD\"}, {\"date\": \"2019-04-04T00:00:00\", \"actual\": 1215.0, \"position\": 1205.920044, \"action\": \"HOLD\"}, {\"date\": \"2019-04-05T00:00:00\", \"actual\": 1207.150024, \"position\": 1215.0, \"action\": \"HOLD\"}, {\"date\": \"2019-04-08T00:00:00\", \"actual\": 1203.839966, \"position\": 1207.150024, \"action\": \"BUY\"}, {\"date\": \"2019-04-09T00:00:00\", \"actual\": 1197.25, \"position\": 1203.839966, \"action\": \"BUY\"}, {\"date\": \"2019-04-10T00:00:00\", \"actual\": 1202.160034, \"position\": 1197.25, \"action\": \"BUY\"}, {\"date\": \"2019-04-11T00:00:00\", \"actual\": 1204.619995, \"position\": 1202.160034, \"action\": \"SELL\"}, {\"date\": \"2019-04-12T00:00:00\", \"actual\": 1217.869995, \"position\": 1204.619995, \"action\": \"SELL\"}, {\"date\": \"2019-04-15T00:00:00\", \"actual\": 1221.099976, \"position\": 1217.869995, \"action\": \"SELL\"}, {\"date\": \"2019-04-16T00:00:00\", \"actual\": 1227.130005, \"position\": 1221.099976, \"action\": \"HOLD\"}, {\"date\": \"2019-04-17T00:00:00\", \"actual\": 1236.339966, \"position\": 1227.130005, \"action\": \"HOLD\"}, {\"date\": \"2019-04-18T00:00:00\", \"actual\": 1236.369995, \"position\": 1236.339966, \"action\": \"HOLD\"}, {\"date\": \"2019-04-22T00:00:00\", \"actual\": 1248.839966, \"position\": 1236.369995, \"action\": \"HOLD\"}, {\"date\": \"2019-04-23T00:00:00\", \"actual\": 1264.550049, \"position\": 1248.839966, \"action\": \"HOLD\"}, {\"date\": \"2019-04-24T00:00:00\", \"actual\": 1256.0, \"position\": 1264.550049, \"action\": \"HOLD\"}, {\"date\": \"2019-04-25T00:00:00\", \"actual\": 1263.449951, \"position\": 1256.0, \"action\": \"HOLD\"}, {\"date\": \"2019-04-26T00:00:00\", \"actual\": 1272.180054, \"position\": 1263.449951, \"action\": \"HOLD\"}, {\"date\": \"2019-04-29T00:00:00\", \"actual\": 1287.579956, \"position\": 1272.180054, \"action\": \"HOLD\"}, {\"date\": \"2019-04-30T00:00:00\", \"actual\": 1188.47998, \"position\": 1287.579956, \"action\": \"HOLD\"}, {\"date\": \"2019-05-01T00:00:00\", \"actual\": 1168.079956, \"position\": 1188.47998, \"action\": \"HOLD\"}, {\"date\": \"2019-05-02T00:00:00\", \"actual\": 1162.609985, \"position\": 1168.079956, \"action\": \"HOLD\"}, {\"date\": \"2019-05-03T00:00:00\", \"actual\": 1185.400024, \"position\": 1162.609985, \"action\": \"BUY\"}, {\"date\": \"2019-05-06T00:00:00\", \"actual\": 1189.390015, \"position\": 1185.400024, \"action\": \"SELL\"}, {\"date\": \"2019-05-07T00:00:00\", \"actual\": 1174.099976, \"position\": 1189.390015, \"action\": \"HOLD\"}, {\"date\": \"2019-05-08T00:00:00\", \"actual\": 1166.27002, \"position\": 1174.099976, \"action\": \"BUY\"}, {\"date\": \"2019-05-09T00:00:00\", \"actual\": 1162.380005, \"position\": 1166.27002, \"action\": \"BUY\"}, {\"date\": \"2019-05-10T00:00:00\", \"actual\": 1164.27002, \"position\": 1162.380005, \"action\": \"HOLD\"}, {\"date\": \"2019-05-13T00:00:00\", \"actual\": 1132.030029, \"position\": 1164.27002, \"action\": \"SELL\"}, {\"date\": \"2019-05-14T00:00:00\", \"actual\": 1120.439941, \"position\": 1132.030029, \"action\": \"HOLD\"}, {\"date\": \"2019-05-15T00:00:00\", \"actual\": 1164.209961, \"position\": 1120.439941, \"action\": \"BUY\"}, {\"date\": \"2019-05-16T00:00:00\", \"actual\": 1178.97998, \"position\": 1164.209961, \"action\": \"BUY\"}, {\"date\": \"2019-05-17T00:00:00\", \"actual\": 1162.300049, \"position\": 1178.97998, \"action\": \"BUY\"}, {\"date\": \"2019-05-20T00:00:00\", \"actual\": 1138.849976, \"position\": 1162.300049, \"action\": \"HOLD\"}, {\"date\": \"2019-05-21T00:00:00\", \"actual\": 1149.630005, \"position\": 1138.849976, \"action\": \"HOLD\"}, {\"date\": \"2019-05-22T00:00:00\", \"actual\": 1151.420044, \"position\": 1149.630005, \"action\": \"BUY\"}, {\"date\": \"2019-05-23T00:00:00\", \"actual\": 1140.77002, \"position\": 1151.420044, \"action\": \"SELL\"}, {\"date\": \"2019-05-24T00:00:00\", \"actual\": 1133.469971, \"position\": 1140.77002, \"action\": \"SELL\"}, {\"date\": \"2019-05-28T00:00:00\", \"actual\": 1134.150024, \"position\": 1133.469971, \"action\": \"BUY\"}, {\"date\": \"2019-05-29T00:00:00\", \"actual\": 1116.459961, \"position\": 1134.150024, \"action\": \"BUY\"}, {\"date\": \"2019-05-30T00:00:00\", \"actual\": 1117.949951, \"position\": 1116.459961, \"action\": \"BUY\"}, {\"date\": \"2019-05-31T00:00:00\", \"actual\": 1103.630005, \"position\": 1117.949951, \"action\": \"BUY\"}, {\"date\": \"2019-06-03T00:00:00\", \"actual\": 1036.22998, \"position\": 1103.630005, \"action\": \"BUY\"}, {\"date\": \"2019-06-04T00:00:00\", \"actual\": 1053.050049, \"position\": 1036.22998, \"action\": \"BUY\"}, {\"date\": \"2019-06-05T00:00:00\", \"actual\": 1042.219971, \"position\": 1053.050049, \"action\": \"SELL\"}, {\"date\": \"2019-06-06T00:00:00\", \"actual\": 1044.339966, \"position\": 1042.219971, \"action\": \"BUY\"}, {\"date\": \"2019-06-07T00:00:00\", \"actual\": 1066.040039, \"position\": 1044.339966, \"action\": \"BUY\"}, {\"date\": \"2019-06-10T00:00:00\", \"actual\": 1080.380005, \"position\": 1066.040039, \"action\": \"HOLD\"}, {\"date\": \"2019-06-11T00:00:00\", \"actual\": 1078.719971, \"position\": 1080.380005, \"action\": \"SELL\"}, {\"date\": \"2019-06-12T00:00:00\", \"actual\": 1077.030029, \"position\": 1078.719971, \"action\": \"BUY\"}, {\"date\": \"2019-06-13T00:00:00\", \"actual\": 1088.77002, \"position\": 1077.030029, \"action\": \"SELL\"}, {\"date\": \"2019-06-14T00:00:00\", \"actual\": 1085.349976, \"position\": 1088.77002, \"action\": \"BUY\"}, {\"date\": \"2019-06-17T00:00:00\", \"actual\": 1092.5, \"position\": 1085.349976, \"action\": \"SELL\"}, {\"date\": \"2019-06-18T00:00:00\", \"actual\": 1103.599976, \"position\": 1092.5, \"action\": \"SELL\"}, {\"date\": \"2019-06-19T00:00:00\", \"actual\": 1102.329956, \"position\": 1103.599976, \"action\": \"BUY\"}, {\"date\": \"2019-06-20T00:00:00\", \"actual\": 1111.420044, \"position\": 1102.329956, \"action\": \"SELL\"}, {\"date\": \"2019-06-21T00:00:00\", \"actual\": 1121.880005, \"position\": 1111.420044, \"action\": \"SELL\"}, {\"date\": \"2019-06-24T00:00:00\", \"actual\": 1115.52002, \"position\": 1121.880005, \"action\": \"BUY\"}, {\"date\": \"2019-06-25T00:00:00\", \"actual\": 1086.349976, \"position\": 1115.52002, \"action\": \"BUY\"}, {\"date\": \"2019-06-26T00:00:00\", \"actual\": 1079.800049, \"position\": 1086.349976, \"action\": \"BUY\"}, {\"date\": \"2019-06-27T00:00:00\", \"actual\": 1076.01001, \"position\": 1079.800049, \"action\": \"BUY\"}, {\"date\": \"2019-06-28T00:00:00\", \"actual\": 1080.910034, \"position\": 1076.01001, \"action\": \"BUY\"}, {\"date\": \"2019-07-01T00:00:00\", \"actual\": 1097.949951, \"position\": 1080.910034, \"action\": \"BUY\"}, {\"date\": \"2019-07-02T00:00:00\", \"actual\": 1111.25, \"position\": 1097.949951, \"action\": \"SELL\"}, {\"date\": \"2019-07-03T00:00:00\", \"actual\": 1121.579956, \"position\": 1111.25, \"action\": \"BUY\"}, {\"date\": \"2019-07-05T00:00:00\", \"actual\": 1131.589966, \"position\": 1121.579956, \"action\": \"SELL\"}, {\"date\": \"2019-07-08T00:00:00\", \"actual\": 1116.349976, \"position\": 1131.589966, \"action\": \"SELL\"}, {\"date\": \"2019-07-09T00:00:00\", \"actual\": 1124.829956, \"position\": 1116.349976, \"action\": \"BUY\"}, {\"date\": \"2019-07-10T00:00:00\", \"actual\": 1140.47998, \"position\": 1124.829956, \"action\": \"SELL\"}, {\"date\": \"2019-07-11T00:00:00\", \"actual\": 1144.209961, \"position\": 1140.47998, \"action\": \"SELL\"}, {\"date\": \"2019-07-12T00:00:00\", \"actual\": 1144.900024, \"position\": 1144.209961, \"action\": \"SELL\"}, {\"date\": \"2019-07-15T00:00:00\", \"actual\": 1150.339966, \"position\": 1144.900024, \"action\": \"SELL\"}, {\"date\": \"2019-07-16T00:00:00\", \"actual\": 1153.579956, \"position\": 1150.339966, \"action\": \"SELL\"}, {\"date\": \"2019-07-17T00:00:00\", \"actual\": 1146.349976, \"position\": 1153.579956, \"action\": \"SELL\"}, {\"date\": \"2019-07-18T00:00:00\", \"actual\": 1146.329956, \"position\": 1146.349976, \"action\": \"SELL\"}, {\"date\": \"2019-07-19T00:00:00\", \"actual\": 1130.099976, \"position\": 1146.329956, \"action\": \"SELL\"}, {\"date\": \"2019-07-22T00:00:00\", \"actual\": 1138.069946, \"position\": 1130.099976, \"action\": \"SELL\"}, {\"date\": \"2019-07-23T00:00:00\", \"actual\": 1146.209961, \"position\": 1138.069946, \"action\": \"SELL\"}, {\"date\": \"2019-07-24T00:00:00\", \"actual\": 1137.810059, \"position\": 1146.209961, \"action\": \"SELL\"}, {\"date\": \"2019-07-25T00:00:00\", \"actual\": 1132.119995, \"position\": 1137.810059, \"action\": \"BUY\"}, {\"date\": \"2019-07-26T00:00:00\", \"actual\": 1250.410034, \"position\": 1132.119995, \"action\": \"SELL\"}, {\"date\": \"2019-07-29T00:00:00\", \"actual\": 1239.410034, \"position\": 1250.410034, \"action\": \"SELL\"}, {\"date\": \"2019-07-30T00:00:00\", \"actual\": 1225.140015, \"position\": 1239.410034, \"action\": \"HOLD\"}, {\"date\": \"2019-07-31T00:00:00\", \"actual\": 1216.680054, \"position\": 1225.140015, \"action\": \"BUY\"}, {\"date\": \"2019-08-01T00:00:00\", \"actual\": 1209.01001, \"position\": 1216.680054, \"action\": \"BUY\"}, {\"date\": \"2019-08-02T00:00:00\", \"actual\": 1193.98999, \"position\": 1209.01001, \"action\": \"BUY\"}, {\"date\": \"2019-08-05T00:00:00\", \"actual\": 1152.319946, \"position\": 1193.98999, \"action\": \"BUY\"}, {\"date\": \"2019-08-06T00:00:00\", \"actual\": 1169.949951, \"position\": 1152.319946, \"action\": \"HOLD\"}, {\"date\": \"2019-08-07T00:00:00\", \"actual\": 1173.98999, \"position\": 1169.949951, \"action\": \"HOLD\"}, {\"date\": \"2019-08-08T00:00:00\", \"actual\": 1204.800049, \"position\": 1173.98999, \"action\": \"BUY\"}, {\"date\": \"2019-08-09T00:00:00\", \"actual\": 1188.01001, \"position\": 1204.800049, \"action\": \"SELL\"}, {\"date\": \"2019-08-12T00:00:00\", \"actual\": 1174.709961, \"position\": 1188.01001, \"action\": \"BUY\"}, {\"date\": \"2019-08-13T00:00:00\", \"actual\": 1197.27002, \"position\": 1174.709961, \"action\": \"HOLD\"}, {\"date\": \"2019-08-14T00:00:00\", \"actual\": 1164.290039, \"position\": 1197.27002, \"action\": \"BUY\"}, {\"date\": \"2019-08-15T00:00:00\", \"actual\": 1167.26001, \"position\": 1164.290039, \"action\": \"BUY\"}, {\"date\": \"2019-08-16T00:00:00\", \"actual\": 1177.599976, \"position\": 1167.26001, \"action\": \"BUY\"}, {\"date\": \"2019-08-19T00:00:00\", \"actual\": 1198.449951, \"position\": 1177.599976, \"action\": \"BUY\"}, {\"date\": \"2019-08-20T00:00:00\", \"actual\": 1182.689941, \"position\": 1198.449951, \"action\": \"SELL\"}, {\"date\": \"2019-08-21T00:00:00\", \"actual\": 1191.25, \"position\": 1182.689941, \"action\": \"SELL\"}, {\"date\": \"2019-08-22T00:00:00\", \"actual\": 1189.530029, \"position\": 1191.25, \"action\": \"HOLD\"}, {\"date\": \"2019-08-23T00:00:00\", \"actual\": 1151.290039, \"position\": 1189.530029, \"action\": \"BUY\"}, {\"date\": \"2019-08-26T00:00:00\", \"actual\": 1168.890015, \"position\": 1151.290039, \"action\": \"SELL\"}, {\"date\": \"2019-08-27T00:00:00\", \"actual\": 1167.839966, \"position\": 1168.890015, \"action\": \"BUY\"}, {\"date\": \"2019-08-28T00:00:00\", \"actual\": 1171.02002, \"position\": 1167.839966, \"action\": \"SELL\"}, {\"date\": \"2019-08-29T00:00:00\", \"actual\": 1192.849976, \"position\": 1171.02002, \"action\": \"SELL\"}, {\"date\": \"2019-08-30T00:00:00\", \"actual\": 1188.099976, \"position\": 1192.849976, \"action\": \"SELL\"}, {\"date\": \"2019-09-03T00:00:00\", \"actual\": 1168.390015, \"position\": 1188.099976, \"action\": \"SELL\"}, {\"date\": \"2019-09-04T00:00:00\", \"actual\": 1181.410034, \"position\": 1168.390015, \"action\": \"SELL\"}, {\"date\": \"2019-09-05T00:00:00\", \"actual\": 1211.380005, \"position\": 1181.410034, \"action\": \"SELL\"}, {\"date\": \"2019-09-06T00:00:00\", \"actual\": 1204.930054, \"position\": 1211.380005, \"action\": \"SELL\"}, {\"date\": \"2019-09-09T00:00:00\", \"actual\": 1204.410034, \"position\": 1204.930054, \"action\": \"SELL\"}, {\"date\": \"2019-09-10T00:00:00\", \"actual\": 1206.0, \"position\": 1204.410034, \"action\": \"BUY\"}, {\"date\": \"2019-09-11T00:00:00\", \"actual\": 1220.170044, \"position\": 1206.0, \"action\": \"SELL\"}, {\"date\": \"2019-09-12T00:00:00\", \"actual\": 1234.25, \"position\": 1220.170044, \"action\": \"HOLD\"}, {\"date\": \"2019-09-13T00:00:00\", \"actual\": 1239.560059, \"position\": 1234.25, \"action\": \"HOLD\"}, {\"date\": \"2019-09-16T00:00:00\", \"actual\": 1231.300049, \"position\": 1239.560059, \"action\": \"HOLD\"}, {\"date\": \"2019-09-17T00:00:00\", \"actual\": 1229.150024, \"position\": 1231.300049, \"action\": \"HOLD\"}, {\"date\": \"2019-09-18T00:00:00\", \"actual\": 1232.410034, \"position\": 1229.150024, \"action\": \"BUY\"}, {\"date\": \"2019-09-19T00:00:00\", \"actual\": 1238.709961, \"position\": 1232.410034, \"action\": \"SELL\"}, {\"date\": \"2019-09-20T00:00:00\", \"actual\": 1229.930054, \"position\": 1238.709961, \"action\": \"HOLD\"}, {\"date\": \"2019-09-23T00:00:00\", \"actual\": 1234.030029, \"position\": 1229.930054, \"action\": \"HOLD\"}, {\"date\": \"2019-09-24T00:00:00\", \"actual\": 1218.76001, \"position\": 1234.030029, \"action\": \"HOLD\"}, {\"date\": \"2019-09-25T00:00:00\", \"actual\": 1246.52002, \"position\": 1218.76001, \"action\": \"HOLD\"}, {\"date\": \"2019-09-26T00:00:00\", \"actual\": 1241.390015, \"position\": 1246.52002, \"action\": \"HOLD\"}, {\"date\": \"2019-09-27T00:00:00\", \"actual\": 1225.089966, \"position\": 1241.390015, \"action\": \"HOLD\"}, {\"date\": \"2019-09-30T00:00:00\", \"actual\": 1219.0, \"position\": 1225.089966, \"action\": \"BUY\"}, {\"date\": \"2019-10-01T00:00:00\", \"actual\": 1205.099976, \"position\": 1219.0, \"action\": \"SELL\"}, {\"date\": \"2019-10-02T00:00:00\", \"actual\": 1176.630005, \"position\": 1205.099976, \"action\": \"HOLD\"}, {\"date\": \"2019-10-03T00:00:00\", \"actual\": 1187.829956, \"position\": 1176.630005, \"action\": \"HOLD\"}, {\"date\": \"2019-10-04T00:00:00\", \"actual\": 1209.0, \"position\": 1187.829956, \"action\": \"HOLD\"}, {\"date\": \"2019-10-07T00:00:00\", \"actual\": 1207.680054, \"position\": 1209.0, \"action\": \"BUY\"}, {\"date\": \"2019-10-08T00:00:00\", \"actual\": 1189.130005, \"position\": 1207.680054, \"action\": \"SELL\"}, {\"date\": \"2019-10-09T00:00:00\", \"actual\": 1202.310059, \"position\": 1189.130005, \"action\": \"BUY\"}, {\"date\": \"2019-10-10T00:00:00\", \"actual\": 1208.670044, \"position\": 1202.310059, \"action\": \"BUY\"}, {\"date\": \"2019-10-11T00:00:00\", \"actual\": 1215.449951, \"position\": 1208.670044, \"action\": \"SELL\"}, {\"date\": \"2019-10-14T00:00:00\", \"actual\": 1217.140015, \"position\": 1215.449951, \"action\": \"BUY\"}, {\"date\": \"2019-10-15T00:00:00\", \"actual\": 1243.01001, \"position\": 1217.140015, \"action\": \"HOLD\"}, {\"date\": \"2019-10-16T00:00:00\", \"actual\": 1243.640015, \"position\": 1243.01001, \"action\": \"SELL\"}, {\"date\": \"2019-10-17T00:00:00\", \"actual\": 1253.069946, \"position\": 1243.640015, \"action\": \"SELL\"}, {\"date\": \"2019-10-18T00:00:00\", \"actual\": 1245.48999, \"position\": 1253.069946, \"action\": \"HOLD\"}, {\"date\": \"2019-10-21T00:00:00\", \"actual\": 1246.150024, \"position\": 1245.48999, \"action\": \"BUY\"}, {\"date\": \"2019-10-22T00:00:00\", \"actual\": 1242.800049, \"position\": 1246.150024, \"action\": \"BUY\"}, {\"date\": \"2019-10-23T00:00:00\", \"actual\": 1259.130005, \"position\": 1242.800049, \"action\": \"SELL\"}, {\"date\": \"2019-10-24T00:00:00\", \"actual\": 1260.98999, \"position\": 1259.130005, \"action\": \"SELL\"}, {\"date\": \"2019-10-25T00:00:00\", \"actual\": 1265.130005, \"position\": 1260.98999, \"action\": \"HOLD\"}, {\"date\": \"2019-10-28T00:00:00\", \"actual\": 1290.0, \"position\": 1265.130005, \"action\": \"HOLD\"}, {\"date\": \"2019-10-29T00:00:00\", \"actual\": 1262.619995, \"position\": 1290.0, \"action\": \"HOLD\"}, {\"date\": \"2019-10-30T00:00:00\", \"actual\": 1261.290039, \"position\": 1262.619995, \"action\": \"HOLD\"}, {\"date\": \"2019-10-31T00:00:00\", \"actual\": 1260.109985, \"position\": 1261.290039, \"action\": \"HOLD\"}, {\"date\": \"2019-11-01T00:00:00\", \"actual\": 1273.73999, \"position\": 1260.109985, \"action\": \"HOLD\"}, {\"date\": \"2019-11-04T00:00:00\", \"actual\": 1291.369995, \"position\": 1273.73999, \"action\": \"HOLD\"}, {\"date\": \"2019-11-05T00:00:00\", \"actual\": 1292.030029, \"position\": 1291.369995, \"action\": \"BUY\"}, {\"date\": \"2019-11-06T00:00:00\", \"actual\": 1291.800049, \"position\": 1292.030029, \"action\": \"SELL\"}, {\"date\": \"2019-11-07T00:00:00\", \"actual\": 1308.859985, \"position\": 1291.800049, \"action\": \"HOLD\"}, {\"date\": \"2019-11-08T00:00:00\", \"actual\": 1311.369995, \"position\": 1308.859985, \"action\": \"HOLD\"}, {\"date\": \"2019-11-11T00:00:00\", \"actual\": 1299.189941, \"position\": 1311.369995, \"action\": \"HOLD\"}, {\"date\": \"2019-11-12T00:00:00\", \"actual\": 1298.800049, \"position\": 1299.189941, \"action\": \"HOLD\"}, {\"date\": \"2019-11-13T00:00:00\", \"actual\": 1298.0, \"position\": 1298.800049, \"action\": \"HOLD\"}, {\"date\": \"2019-11-14T00:00:00\", \"actual\": 1311.459961, \"position\": 1298.0, \"action\": \"HOLD\"}, {\"date\": \"2019-11-15T00:00:00\", \"actual\": 1334.869995, \"position\": 1311.459961, \"action\": \"HOLD\"}, {\"date\": \"2019-11-18T00:00:00\", \"actual\": 1320.699951, \"position\": 1334.869995, \"action\": \"HOLD\"}, {\"date\": \"2019-11-19T00:00:00\", \"actual\": 1315.459961, \"position\": 1320.699951, \"action\": \"HOLD\"}, {\"date\": \"2019-11-20T00:00:00\", \"actual\": 1303.050049, \"position\": 1315.459961, \"action\": \"HOLD\"}, {\"date\": \"2019-11-21T00:00:00\", \"actual\": 1301.349976, \"position\": 1303.050049, \"action\": \"BUY\"}, {\"date\": \"2019-11-22T00:00:00\", \"actual\": 1295.339966, \"position\": 1301.349976, \"action\": \"BUY\"}, {\"date\": \"2019-11-25T00:00:00\", \"actual\": 1306.689941, \"position\": 1295.339966, \"action\": \"BUY\"}, {\"date\": \"2019-11-26T00:00:00\", \"actual\": 1313.550049, \"position\": 1306.689941, \"action\": \"HOLD\"}, {\"date\": \"2019-11-27T00:00:00\", \"actual\": 1312.98999, \"position\": 1313.550049, \"action\": \"BUY\"}, {\"date\": \"2019-11-29T00:00:00\", \"actual\": 1304.959961, \"position\": 1312.98999, \"action\": \"BUY\"}, {\"date\": \"2019-12-02T00:00:00\", \"actual\": 1289.920044, \"position\": 1304.959961, \"action\": \"BUY\"}, {\"date\": \"2019-12-03T00:00:00\", \"actual\": 1295.280029, \"position\": 1289.920044, \"action\": \"BUY\"}, {\"date\": \"2019-12-04T00:00:00\", \"actual\": 1320.540039, \"position\": 1295.280029, \"action\": \"SELL\"}, {\"date\": \"2019-12-05T00:00:00\", \"actual\": 1328.130005, \"position\": 1320.540039, \"action\": \"HOLD\"}, {\"date\": \"2019-12-06T00:00:00\", \"actual\": 1340.619995, \"position\": 1328.130005, \"action\": \"SELL\"}, {\"date\": \"2019-12-09T00:00:00\", \"actual\": 1343.560059, \"position\": 1340.619995, \"action\": \"SELL\"}, {\"date\": \"2019-12-10T00:00:00\", \"actual\": 1344.660034, \"position\": 1343.560059, \"action\": \"SELL\"}, {\"date\": \"2019-12-11T00:00:00\", \"actual\": 1345.02002, \"position\": 1344.660034, \"action\": \"SELL\"}, {\"date\": \"2019-12-12T00:00:00\", \"actual\": 1350.27002, \"position\": 1345.02002, \"action\": \"SELL\"}, {\"date\": \"2019-12-13T00:00:00\", \"actual\": 1347.829956, \"position\": 1350.27002, \"action\": \"SELL\"}, {\"date\": \"2019-12-16T00:00:00\", \"actual\": 1361.170044, \"position\": 1347.829956, \"action\": \"HOLD\"}, {\"date\": \"2019-12-17T00:00:00\", \"actual\": 1355.119995, \"position\": 1361.170044, \"action\": \"HOLD\"}, {\"date\": \"2019-12-18T00:00:00\", \"actual\": 1352.619995, \"position\": 1355.119995, \"action\": \"HOLD\"}, {\"date\": \"2019-12-19T00:00:00\", \"actual\": 1356.040039, \"position\": 1352.619995, \"action\": \"HOLD\"}, {\"date\": \"2019-12-20T00:00:00\", \"actual\": 1349.589966, \"position\": 1356.040039, \"action\": \"HOLD\"}, {\"date\": \"2019-12-23T00:00:00\", \"actual\": 1348.839966, \"position\": 1349.589966, \"action\": \"HOLD\"}, {\"date\": \"2019-12-24T00:00:00\", \"actual\": 1343.560059, \"position\": 1348.839966, \"action\": \"BUY\"}, {\"date\": \"2019-12-26T00:00:00\", \"actual\": 1360.400024, \"position\": 1343.560059, \"action\": \"SELL\"}, {\"date\": \"2019-12-27T00:00:00\", \"actual\": 1351.890015, \"position\": 1360.400024, \"action\": \"HOLD\"}, {\"date\": \"2019-12-30T00:00:00\", \"actual\": 1336.140015, \"position\": 1351.890015, \"action\": \"HOLD\"}]}}, {\"mode\": \"vega-lite\"});\n",
       "</script>"
      ],
      "text/plain": [
       "alt.LayerChart(...)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chart = visualize(df, history, title=test_stock)\n",
    "chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
